找传奇、传世资源到传世资源站!

C# 读取eml文件 例子(解析eml文件)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

标题/邮箱/信件内容等

C# 读取eml文件 例子(解析eml文件) C#语言基础-第1张
C# 读取eml文件 例子(解析eml文件) C#语言基础-第2张
using System.IO;using System.Linq;using System.Windows.Forms;using ADODB;using CDO;using System.Diagnostics;using Stream = ADODB.Stream;using System;namespace EmlReader.WinForm{ public partial class EmlViewer : Form { public EmlViewer() { InitializeComponent(); } public void OpenEml(string filepath) { try { var stream = new StreamClass(); stream.Open(); stream.LoadFromFile(filepath); stream.Flush(); var mail = new MessageClass(); mail.DataSource.OpenObject(stream, "_Stream"); mail.DataSource.Save(); fromBox.Text = mail.From; toBox.Text = mail.To; ccBox.Text = mail.CC; bccBox.Text = mail.BCC; subjectBox.Text = mail.Subject; bodyBox.Text = mail.TextBody; attachmentsPanel.Controls.Clear(); foreach (IBodyPart attachment in mail.Attachments) { var linkLabel = new LinkLabel { Text = attachment.FileName, AutoSize = true, }; linkLabel.Links.Add(0, linkLabel.Text.Length, attachment.GetDecodedContentStream()); linkLabel.LinkClicked = LinkLabelLinkClicked; linkLabel.Disposed = LinkLabelDisposed; attachmentsPanel.Controls.Add(linkLabel); } stream.Close(); } catch (Exception) { MessageBox.Show(this, "An error occured while opening the file", "Email could not be opened", MessageBoxButtons.OK, MessageBoxIcon.Error); } } static void LinkLabelDisposed(object sender, EventArgs e) { var linkLabel = sender as LinkLabel; if (linkLabel == null) return; foreach (LinkLabel.Link link in linkLabel.Links) ((Stream)link.LinkData).Close(); } static void LinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { var linkLabel = sender as LinkLabel; if (linkLabel == null) return; var fileName = linkLabel.Text; linkLabel.LinkVisited = true; var stream = e.Link.LinkData as Stream; if (stream == null) return; var tempFile = Path.Combine(Path.GetTempPath(), fileName); stream.SaveToFile(tempFile, SaveOptionsEnum.adSaveCreateOverWrite); Process.Start(tempFile); } private void EmlViewerDragDrop(object sender, DragEventArgs e) { var files = (string[])e.Data.GetData(DataFormats.FileDrop); OpenEml(files.First()); } private void EmlViewerDragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; } private void OpenButtonClick(object sender, EventArgs e) { openFileDialog.ShowDialog(this); } private void OpenFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e) { var dialog = sender as FileDialog; if (dialog == null) return; OpenEml(dialog.FileName); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复